package com.wzz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzz.dto.AddUserDto;
import com.wzz.entity.Coll;
import com.wzz.entity.Notice;
import com.wzz.entity.Question;
import com.wzz.entity.UserRole;
import com.wzz.service.*;
import com.wzz.vo.CommonResult;
import com.wzz.vo.PageResponse;
import com.wzz.vo.QuestionVo;
import com.wzz.vo.UserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author by liutao
 * @implNote 10/20 19:07
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/coll")
public class CollController {

    private final UserService userService;

    private final CollService collService;

    private final NoticeService noticeService;

    private final QuestionService questionService;

    @GetMapping("/getAllColl")
    public CommonResult<PageResponse<Coll>> getAllNotice(Coll coll) {
        PageResponse response =collService.getAllColl(coll);
        List<Coll> collList = response.getData();
        for (Coll coll1 : collList) {

            if(coll1.getType().equals("收藏")  && coll1.getField1().equals("bc")){

                Question question2 = questionService.getById(coll1.getDataid());
                if(question2==null){
                    continue;
                }
                Coll coll2 = collService.getOne(new LambdaQueryWrapper<Coll>().eq(Coll::getId,coll1.getDataid()));
                if(coll2!=null){
                    coll1.setContent(coll2.getName());
                }

            }else
            if(coll1.getType().equals("收藏") ){

                Question question2 = questionService.getById(coll1.getDataid());
                if(question2==null){
                    continue;
                }
                QuestionVo question = questionService.getQuestionVoById(coll1.getDataid());
                if(question!=null){
                    coll1.setContent(question.getQuestionContent());
                }
            }
        }
        return CommonResult.<PageResponse<Coll>>builder()
                .data(response)
                .build();
    }

    @PostMapping("/addColl")
    public CommonResult<Void> addColl(@RequestBody Coll coll) {
        coll.setCtime(new Date());
        collService.save(coll);
        return CommonResult.<Void>builder()
                .message(coll.getId()+"")
                .build();
    }

    @GetMapping("/deleteColl")
    @ApiOperation("批量删除 ")
    public CommonResult<Void> deleteColl(@RequestParam(name = "ids") String noticeIds) {
        String[] ids = noticeIds.split(",");
        collService.removeByIds(Arrays.asList(ids));
        return CommonResult.<Void>builder().build();
    }

    @PostMapping("/updateColl")
    @ApiOperation("更新")
    public CommonResult<Void> updateColl(@RequestBody Coll coll) {
        collService.updateById(coll);
        return CommonResult.<Void>builder().build();
    }
}
